package com.czy.workflow.core.service.impl;

import com.czy.workflow.core.model.*;
import com.czy.workflow.core.service.FlowService;
import com.czy.workflow.core.service.dao.BranchRepository;
import com.czy.workflow.core.service.dao.FlowRuntimeNodeRepository;
import com.czy.workflow.core.service.dao.MainRepository;
import com.czy.workflow.core.service.dao.NodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by admin on 2019/8/31.
 */


@Transactional
@Service("mysqlFlowService")
public class MysqlFlowService implements FlowService {

    @Autowired
    MainRepository mainRepository;

    @Autowired
    NodeRepository nodeRepository;

    @Autowired
    BranchRepository branchRepository;

    @Autowired
    FlowRuntimeNodeRepository runtimeNodeRepository;

    @Override
    public boolean saveFlowMain(FlowMain flowMain) {
        return mainRepository.save(flowMain) != null;
    }

    @Override
    public FlowMain getFlowMainById(String flowId) {
        return mainRepository.findById(flowId).orElse(null);
    }

    @Override
    public FlowNode getFlowNodeById(String flowId, String nodeId) {
        return nodeRepository.findById(nodeId).orElse(null);
    }

    @Override
    public List<FlowNode> getNextNodes(String flowId, String nodeId) {
        return nodeRepository.findNextNodes(nodeId);
    }

    @Override
    public List<FlowBranch> getNodeBranches(String flowId, String nodeId){
        return branchRepository.findNoteBranches(nodeId);
    }

    @Override
    public List<FlowNode> getStartNodes(String flowId) {
        return nodeRepository.findByFlowIdAndOperationType(flowId, OperationType.START);
    }

    @Override
    public boolean refreshCurrentNodes(String docId, String currentNodeId, List<FlowNode> nodes) {
        runtimeNodeRepository.deleteByDocIdAndNodeId(docId, currentNodeId);
        for(FlowNode node : nodes){
            runtimeNodeRepository.save(new FlowRuntimeNode(docId, node.getId()));
        }
        return true;
    }

    @Override
    public boolean clearCurrentNodes(String docId) {
        return runtimeNodeRepository.deleteByDocId(docId) >= 0;
    }

    @Override
    public boolean existCurrentNode(String docId, String currentNodeId) {
        return runtimeNodeRepository.existsByDocIdAndNodeId(docId, currentNodeId);
    }

    @Override
    public List<FlowNode> getCurrentNodes(String docId) {
        return nodeRepository.findCurrentNodes(docId);
    }

    @Override
    public FlowOrgElement getCurrentUser() {
        return null;
    }

    @Override
    public boolean saveFlowLog(TaskInfo taskInfo, boolean result) {
        return true;
    }

    @Override
    public TaskInfo getTaskInfoByDocId(String docId, String nodeId) {
        FlowNode node = nodeRepository.findById(nodeId).orElse(null);
        if(node == null){
            return null;
        }

        FlowMain flowMain = this.getFlowMainById(node.getFlowId());

        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setNode(node);
        taskInfo.setFlowId(flowMain.getId());
        taskInfo.setFlowCode(flowMain.getCode());
        taskInfo.setDocId(docId);

        return taskInfo;
    }

    @Override
    public boolean validateNodeAuthority(TaskInfo taskInfo) {
        //
        return true;
    }
}
